<?php
// 事务  是一系列相关的数据库操作，要么全做 要么全不做。原子性
// start transaction 开启事务    rollback 回滚操作   commit 提交

try{
    $dsn = 'mysql:host=localhost;dbname=shumei19;charset=utf8';
    $pdo = new PDO($dsn, 'root', '654321');
    $pdo->beginTransaction();
    // id为1的用户给id为2用户转账500元
    $sql = 'update users set money = money - 500 where id = 1';
    $result = $pdo->exec($sql);
    if (!$result) {
        $pdo->rollBack();
        throw new PDOException('转出失败1');
    }

    $sql = 'update users set money = money + 500 where id = 2';
    $result = $pdo->exec($sql);
    if (!$result) {
        $pdo->rollBack();
        throw new PDOException('转出失败2');
    }

    $pdo->commit();
}catch (PDOException $e) {
    exit($e->getMessage());
}